import {ref} from 'vue'
export const rules = {
  useEmail: /[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/,
  verCode: '',
  password: ''
}

export interface ruleType {
  rulesText: string,
  isRules: boolean
}
export let ruleTem:ruleType = {
  rulesText: '',
  isRules: false
}
//输入框规则
export const SetRules = (type: string, inputText: string | number): ruleType => {
  const rule: ruleType = {
    rulesText: '',
    isRules: false
  }
  switch (type) {
    case 'email':
      if (!inputText) {
        rule.rulesText = '请输入邮箱'
        rule.isRules = true
        return rule
      }
      if (!rules.useEmail.test('' + inputText)) {
        rule.isRules = true
        rule.rulesText = '邮箱地址错误'
        return rule
      }

      rule.isRules = false
      return rule
    case 'verCode':
      if (!inputText) {
        rule.isRules = true
        rule.rulesText = '请输入验证码'
        return rule
      }
      rule.isRules = false
      rule.rulesText = ''
      return rule
    case 'password':
      //密码验证
      if(!inputText) {
        rule.isRules = true
        rule.rulesText = '请输入密码'
        return rule
      }
      if (inputText && (inputText as string).length < 6) {
        rule.isRules = true
        rule.rulesText = '密码最小为6位'
        return rule;
      }
      rule.isRules = false
      rule.rulesText = ''
      return rule;
    default:
      return rule
  }
}

